package com.lc.soft.service.impl;

import com.lc.soft.dao.IMapDao;
import com.lc.soft.entity.CommonParameter;
import com.lc.soft.entity.MapEntity;
import com.lc.soft.service.IMapService;
import com.lc.soft.utils.CommonUtils;
import com.lc.soft.utils.ResponseResult;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Log4j2
public class MapService implements IMapService {

    private IMapDao mapDao;

    @Autowired
    public MapService(IMapDao mapDao) {
        this.mapDao = mapDao;
    }

    @Override
    public ResponseResult create(MapEntity mapEntity) {
        if (StringUtils.isBlank(mapEntity.getGeojson())) {
            return ResponseResult.PARAMETER_ERROR;
        }
        try {
            MapEntity mapEntity1 = mapDao.findMapByCondition(mapEntity);
            if (mapEntity1 != null) {
                return ResponseResult.SETTING_ALREADY_EXISTS;
            }
            MapEntity mapEntity2 = CommonUtils.createMapEntity(mapEntity);
            if (mapEntity2 == null) {
                return ResponseResult.PARAMETER_ERROR;
            }
            int createResult = mapDao.create(mapEntity);
            if (createResult == 0) {
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult update(MapEntity mapEntity) {
        if (StringUtils.isBlank(mapEntity.getGeojson())) {
            return ResponseResult.PARAMETER_ERROR;
        }
        try {
            MapEntity mapEntity1 = CommonUtils.createMapEntity(mapEntity);
            if (mapEntity1 == null) {
                return ResponseResult.PARAMETER_ERROR;
            }
            int updateResult = mapDao.update(mapEntity);
            if (updateResult == 0) {
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult updateState(MapEntity mapEntity) {
        try {
            int updateResult = mapDao.updateState(mapEntity);
            if (updateResult == 0) {
                return ResponseResult.OPERATION_FAILED;
            }
            return ResponseResult.success();
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }

    @Override
    public ResponseResult findPagedMapListByCondition(CommonParameter commonParameter) {
        try {
            List<MapEntity> mapEntityList = mapDao.findPagedMapListByCondition(commonParameter);
            if (mapEntityList == null || mapEntityList.size() == 0) {
                return ResponseResult.DATA_NOT_AVAILABLE;
            }
            int amount = mapDao.findAmountByCondition(commonParameter);
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("list", mapEntityList);
            resultMap.put("amount", amount);
            return ResponseResult.success(resultMap);
        } catch (Exception e) {
            log.error(e);
            return ResponseResult.UNKNOWN_ERROR;
        }
    }
}
